package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.WorkflowBusinessUserRelRpcService;
import com.hunttown.mes.rpc.domain.WorkflowBusinessUserRelDTO;
import com.hunttown.mes.rpc.domain.query.WorkflowBusinessUserRelDTOQuery;
import com.hunttown.mes.service.WorkflowBusinessUserRelService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.WorkflowBusinessUserRelConverts.convert;

/**
 * created by wangjunfu on 2020-12-08 10:50:35
 */
@Service
public class WorkflowBusinessUserRelRpcServiceImpl implements WorkflowBusinessUserRelRpcService {

    private final Logger logger = LoggerFactory.getLogger(WorkflowBusinessUserRelRpcServiceImpl.class);

    private final WorkflowBusinessUserRelService workflowBusinessUserRelService;

    @Autowired
    public WorkflowBusinessUserRelRpcServiceImpl(WorkflowBusinessUserRelService workflowBusinessUserRelService) {
        this.workflowBusinessUserRelService = workflowBusinessUserRelService;
    }

    @Override
    public WorkflowBusinessUserRelDTO insert(WorkflowBusinessUserRelDTO workflowBusinessUserRelDTO) {
        try {
            return convert(workflowBusinessUserRelService.insert(convert(workflowBusinessUserRelDTO)));
        } catch (Exception e) {
            logger.error("WorkflowBusinessUserRel create failed.", e);
            throw new RuntimeException("WorkflowBusinessUserRel create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(WorkflowBusinessUserRelDTO workflowBusinessUserRelDTO) {
        try {
            return workflowBusinessUserRelService.updateInfoById(convert(workflowBusinessUserRelDTO));
        } catch (Exception e) {
            logger.error("WorkflowBusinessUserRel update by id failed.", e);
            throw new RuntimeException("WorkflowBusinessUserRel update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(WorkflowBusinessUserRelDTOQuery workflowBusinessUserRelDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (workflowBusinessUserRelDTOQuery.getW_id() != null && workflowBusinessUserRelDTOQuery.getW_id() > 0) {
                query.put("w_id", workflowBusinessUserRelDTOQuery.getW_id());
            }

            if (workflowBusinessUserRelDTOQuery.getW_nodeId() != null && workflowBusinessUserRelDTOQuery.getW_nodeId() > 0) {
                query.put("w_nodeId", workflowBusinessUserRelDTOQuery.getW_nodeId());
            }

            if (workflowBusinessUserRelDTOQuery.getW_deleteFlag() != null) {
                query.put("w_deleteFlag", workflowBusinessUserRelDTOQuery.getW_deleteFlag());
            }

            if (workflowBusinessUserRelDTOQuery.getW_businessId() != null) {
                query.put("w_businessId", workflowBusinessUserRelDTOQuery.getW_businessId());
            }

            if (workflowBusinessUserRelDTOQuery.getIds() != null && !workflowBusinessUserRelDTOQuery.getIds().equals("")) {
                query.put("ids", workflowBusinessUserRelDTOQuery.getIds());
            }

            if (workflowBusinessUserRelDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", workflowBusinessUserRelDTOQuery.getStopFlag());
            }

            if (workflowBusinessUserRelDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", workflowBusinessUserRelDTOQuery.getDeleteFlag());
            }

            return workflowBusinessUserRelService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("WorkflowBusinessUserRel update by query failed.", e);
            throw new RuntimeException("WorkflowBusinessUserRel update by query failed, Please order your code.", e);
        }
    }

    @Override
    public WorkflowBusinessUserRelDTO selectById(Integer id) {
        try {
            return convert(workflowBusinessUserRelService.selectById(id));
        } catch (Exception e) {
            logger.error("WorkflowBusinessUserRel select by id failed.", e);
            throw new RuntimeException("WorkflowBusinessUserRel select by id failed, Please order your code.", e);
        }
    }

    @Override
    public WorkflowBusinessUserRelDTO selectObjByQuery(WorkflowBusinessUserRelDTOQuery workflowBusinessUserRelDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            return convert(workflowBusinessUserRelService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("WorkflowBusinessUserRel select obj by query failed.", e);
            throw new RuntimeException("WorkflowBusinessUserRel select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<WorkflowBusinessUserRelDTO> getListForPage(WorkflowBusinessUserRelDTOQuery workflowBusinessUserRelDTOQuery) {
        Query query = Query.page(workflowBusinessUserRelDTOQuery.getPn(), workflowBusinessUserRelDTOQuery.getPageSize());

        if (workflowBusinessUserRelDTOQuery.getId() != null && workflowBusinessUserRelDTOQuery.getId() > 0) {
            query.put("id", workflowBusinessUserRelDTOQuery.getId());
        }

        if (workflowBusinessUserRelDTOQuery.getIds() != null && !workflowBusinessUserRelDTOQuery.getIds().equals("")) {
            query.put("ids", workflowBusinessUserRelDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(workflowBusinessUserRelDTOQuery.getLikeName())) {
            query.put("likeName", workflowBusinessUserRelDTOQuery.getLikeName());
        }

        if (workflowBusinessUserRelDTOQuery.getNodeId() != null) {
            query.put("nodeId", workflowBusinessUserRelDTOQuery.getNodeId());
        }

        if (StringUtils.isNotBlank(workflowBusinessUserRelDTOQuery.getShowDate())) {
            query.put("showDate", workflowBusinessUserRelDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(workflowBusinessUserRelDTOQuery.getStartDate())) {
            query.put("startDate", workflowBusinessUserRelDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(workflowBusinessUserRelDTOQuery.getEndDate())) {
            query.put("endDate", workflowBusinessUserRelDTOQuery.getEndDate());
        }

        if (StringUtils.isNotBlank(workflowBusinessUserRelDTOQuery.getOrderBy())) {
            query.put("orderBy", workflowBusinessUserRelDTOQuery.getOrderBy());
        }

        List<WorkflowBusinessUserRelDTO> list = convert(workflowBusinessUserRelService.selectByQuery(query));
        Page<WorkflowBusinessUserRelDTO> page = Page.from(workflowBusinessUserRelDTOQuery.getPn(), workflowBusinessUserRelDTOQuery.getPageSize(), list);
        page.setTotalItems(workflowBusinessUserRelService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return workflowBusinessUserRelService.deleteById(id);
        } catch (Exception e) {
            logger.error("WorkflowBusinessUserRel delete failed.", e);
            throw new RuntimeException("WorkflowBusinessUserRel delete failed, Please order your code.", e);
        }
    }
}